package com.wbu.estatemanagement.controller;

import com.github.pagehelper.Page;
import com.wbu.estatemanagement.common.MessageConstant;
import com.wbu.estatemanagement.common.PageResult;
import com.wbu.estatemanagement.common.Result;
import com.wbu.estatemanagement.common.StatusCode;
import com.wbu.estatemanagement.dao.HouseMapper;
import com.wbu.estatemanagement.domain.Building;
import com.wbu.estatemanagement.domain.House;
import com.wbu.estatemanagement.domain.User;
import com.wbu.estatemanagement.service.BuildingService;
import com.wbu.estatemanagement.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("house")
public class HouseController {

    @Autowired
    HouseService houseService;


    @PostMapping("/userHouse")
    public Result userHouse(HttpSession httpSession){
        User user= (User) httpSession.getAttribute("user");
        Integer userHouseId = user.getHouseId();
        House house=houseService.findHouse(userHouseId);
        return new Result(true, StatusCode.OK, MessageConstant.SUCCESS,house);
    }

    @RequestMapping("/find")
    public Result find(){
        List<House> all = houseService.findAll();
        return new Result(false,200,"请求成功",all);
    }
    @RequestMapping("/search")
    public PageResult search(@RequestBody Map searchMap){
        Page<House> page = houseService.search(searchMap);
        return new PageResult(true, StatusCode.OK, MessageConstant.COMMUNITY_SEARCH_SUCCESS,page.getResult(),page.getTotal());
    }

    @RequestMapping("/add")
    public Result add(@RequestBody House house){
        Boolean add = houseService.add(house);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_ADD_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        House community = houseService.findById(id);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_FIND_BY_ID_SUCCESS,community);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody House house){
        Boolean add = houseService.update(house);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_UPDATE_SUCCESS);
    }

    /**
     * 批量删除/单个删除
     * @param ids
     * @return
     */
    @RequestMapping("/del")
    public Result del(@RequestBody List<Integer> ids){
        Boolean flag = houseService.del(ids);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_DELETE_SUCCESS);
    }
}
